package com.jcl.recruit.resume.logic;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.resume.model.*;
import com.jcl.recruit.resume.vo.ResumeResultEntity;
import com.jcl.recruit.resume.vo.ResumeSimpleResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ResumeCommonLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/20 18:05
 * @Version 1.0
 **/
@Component
public class ResumeCommonLogic {

    @Autowired
    private ResumeBasicInfoLogic resumeBasicInfoLogic;

    @Autowired
    private ResumePersInfoLogic resumePersInfoLogic;

    @Autowired
    private ResumeSelfEvaluationLogic resumeSelfEvaluationLogic;

    @Autowired
    private ResumeJobIntentionLogic resumeJobIntentionLogic;

    @Autowired
    private ResumeWorkLogic resumeWorkLogic;

    @Autowired
    private ResumeEduLogic resumeEduLogic;

    @Autowired
    private ResumeProjectLogic resumeProjectLogic;

    public List<ResumeSimpleResultEntity> findSimpleList(String corpCode, List<Integer> resumeIds) {
        List<ResumeSimpleResultEntity> list = new ArrayList<>();
        List<ResumeBasicInfo> entityList = resumeBasicInfoLogic.findList(resumeIds);
        if (CollectionUtils.isNotEmpty(entityList)) {

            Map<Integer, ResumeJobIntention> jobIntentionMap =
                    resumeJobIntentionLogic.findEntityMapByResumeIds(corpCode, resumeIds);
            Map<Integer, ResumePersInfo> persInfoMap =
                    resumePersInfoLogic.findEntityMapByResumeIds(corpCode, resumeIds);

            for (ResumeBasicInfo resumeBasicInfo : entityList) {
                ResumeSimpleResultEntity resumeResultEntity = new ResumeSimpleResultEntity();
                ResumePersInfo persInfo = persInfoMap.get(resumeBasicInfo.getId());
                ResumeJobIntention jobIntention = jobIntentionMap.get(resumeBasicInfo.getId());
                //
                BeanUtils.copyProperties(persInfo, resumeResultEntity);
                BeanUtils.copyProperties(jobIntention, resumeResultEntity);

                resumeResultEntity.setApplyDate(resumeBasicInfo.getApplyDate());
                resumeResultEntity.setUpdateDate(resumeBasicInfo.getUpdateDate());
                resumeResultEntity.setApplyDepartment(resumeBasicInfo.getApplyDepartment());
                resumeResultEntity.setApplyPosition(resumeBasicInfo.getApplyPosition());
                resumeResultEntity.setSourceSub(resumeBasicInfo.getSourceSub());
                resumeResultEntity.setStatus(resumeBasicInfo.getStatus());
                resumeResultEntity.setId(resumeBasicInfo.getId());
                list.add(resumeResultEntity);
            }
        }
        return list;
    }


    public List<ResumeResultEntity> findList(String corpCode, List<Integer> resumeIds) {
        List<ResumeResultEntity> list = new ArrayList<>();
        List<ResumeBasicInfo> entityList = resumeBasicInfoLogic.findList(resumeIds);
        if (CollectionUtils.isNotEmpty(entityList)) {

            Map<Integer, ResumePersInfo> persInfoMap =
                    resumePersInfoLogic.findEntityMapByResumeIds(corpCode, resumeIds);
            Map<Integer, ResumeSelfEvaluation> selfEvaluationMap =
                    resumeSelfEvaluationLogic.findEntityMapByResumeIds(corpCode, resumeIds);
            Map<Integer, ResumeJobIntention> jobIntentionMap =
                    resumeJobIntentionLogic.findEntityMapByResumeIds(corpCode, resumeIds);
            Map<Integer, List<ResumeWork>> worksMap =
                    resumeWorkLogic.findListByResumeMap(corpCode, resumeIds);
            Map<Integer, List<ResumeEdu>> edusMap =
                    resumeEduLogic.findListByResumeMap(corpCode, resumeIds);
            Map<Integer, List<ResumeProject>> projectsMap =
                    resumeProjectLogic.findListByResumeMap(corpCode, resumeIds);

            for (ResumeBasicInfo ResumeBasicInfo : entityList) {

                ResumeResultEntity resumeResultEntity = new ResumeResultEntity();

                ResumePersInfo persInfo = persInfoMap.get(ResumeBasicInfo.getId());
                ResumeSelfEvaluation ResumeSelfEvaluation =
                        selfEvaluationMap.get(ResumeBasicInfo.getId());
                ResumeJobIntention jobIntention = jobIntentionMap.get(ResumeBasicInfo.getId());
                List<ResumeWork> works = worksMap.get(ResumeBasicInfo.getId());
                List<ResumeEdu> edus = edusMap.get(ResumeBasicInfo.getId());
                List<ResumeProject> projects = projectsMap.get(ResumeBasicInfo.getId());

                resumeResultEntity.setBasicInfo(persInfo);
                resumeResultEntity.setSelfEvaluation(ResumeSelfEvaluation);
                resumeResultEntity.setJobIntention(jobIntention);
                resumeResultEntity.setWorks(works);
                resumeResultEntity.setEducations(edus);
                resumeResultEntity.setProjects(projects);

                resumeResultEntity.setApplyDate(ResumeBasicInfo.getApplyDate());
                resumeResultEntity.setUpdateDate(ResumeBasicInfo.getUpdateDate());
                resumeResultEntity.setApplyDepartment(ResumeBasicInfo.getApplyDepartment());
                resumeResultEntity.setApplyPosition(ResumeBasicInfo.getApplyPosition());
                resumeResultEntity.setSourceSub(ResumeBasicInfo.getSourceSub());
                resumeResultEntity.setStatus(ResumeBasicInfo.getStatus());

                resumeResultEntity.setId(ResumeBasicInfo.getId());

                list.add(resumeResultEntity);
            }
        }

        return list;
    }

    public ResumeSimpleResultEntity findSimpleEntity(String corpCode, Integer resumeId) {

        ResumeSimpleResultEntity resumeResultEntity = new ResumeSimpleResultEntity();
        ResumeBasicInfo resumeBasicInfo = resumeBasicInfoLogic.findOne(resumeId);

        ResumePersInfo persInfo = resumePersInfoLogic.findOne(corpCode, resumeBasicInfo.getId());
        ResumeJobIntention jobIntention = resumeJobIntentionLogic.findOne(corpCode, resumeBasicInfo.getId());
        //
        BeanUtils.copyProperties(persInfo, resumeResultEntity);
        BeanUtils.copyProperties(jobIntention, resumeResultEntity);

        resumeResultEntity.setApplyDate(resumeBasicInfo.getApplyDate());
        resumeResultEntity.setUpdateDate(resumeBasicInfo.getUpdateDate());
        resumeResultEntity.setApplyDepartment(resumeBasicInfo.getApplyDepartment());
        resumeResultEntity.setApplyPosition(resumeBasicInfo.getApplyPosition());
        resumeResultEntity.setSourceSub(resumeBasicInfo.getSourceSub());
        resumeResultEntity.setStatus(resumeBasicInfo.getStatus());

        resumeResultEntity.setId(resumeBasicInfo.getId());
        return resumeResultEntity;
    }

    public ResumeResultEntity findDetailEntity(String corpCode, Integer resumeId) {

        return findOne(corpCode, resumeId, false);
    }

    public ResumeResultEntity findOne(String corpCode, Integer resumeId, boolean isClearDuty) {
        ResumeResultEntity resumeResultEntity = null;

        ResumeBasicInfo ResumeBasicInfo = resumeBasicInfoLogic.findOne(resumeId);
        if (null != ResumeBasicInfo) {
            resumeResultEntity = new ResumeResultEntity();
            ResumePersInfo persInfo = resumePersInfoLogic.findOne(corpCode, resumeId);
            ResumeSelfEvaluation resumeSelfEvaluation = resumeSelfEvaluationLogic.findOne(corpCode, resumeId);
            ResumeJobIntention jobIntention = resumeJobIntentionLogic.findOne(corpCode, resumeId);
            List<ResumeWork> works = resumeWorkLogic.findListByResumeId(corpCode, resumeId);
            List<ResumeEdu> edus = resumeEduLogic.findListByResumeId(corpCode, resumeId);
            List<ResumeProject> projects = resumeProjectLogic.findListByResumeId(corpCode, resumeId);

            if (isClearDuty) {
                if (CollectionUtils.isNotEmpty(works)) {
                    for (ResumeWork entity : works) {
                        entity.setDuty(null);
                    }
                }
                if (CollectionUtils.isNotEmpty(projects)) {
                    for (ResumeProject entity : projects) {
                        entity.setDuty(null);
                    }
                }
            }
            resumeResultEntity.setBasicInfo(persInfo);
            resumeResultEntity.setSelfEvaluation(resumeSelfEvaluation);
            resumeResultEntity.setJobIntention(jobIntention);
            resumeResultEntity.setWorks(works);
            resumeResultEntity.setEducations(edus);
            resumeResultEntity.setProjects(projects);

            resumeResultEntity.setApplyDate(ResumeBasicInfo.getApplyDate());
            resumeResultEntity.setUpdateDate(ResumeBasicInfo.getUpdateDate());
            resumeResultEntity.setApplyDepartment(ResumeBasicInfo.getApplyDepartment());
            resumeResultEntity.setApplyPosition(ResumeBasicInfo.getApplyPosition());
            resumeResultEntity.setSourceSub(ResumeBasicInfo.getSourceSub());
            resumeResultEntity.setStatus(ResumeBasicInfo.getStatus());

            resumeResultEntity.setId(ResumeBasicInfo.getId());
        }

        return resumeResultEntity;
    }

    public Map<String, Object> findOneMap(String corpCode, Integer resumeId, boolean isClearDuty) {

        Map<String, Object> dataMap = resumeBasicInfoLogic.findOneMap(resumeId);
        if (null != dataMap) {
            Map<String, Object> persInfo = resumePersInfoLogic.findOneMap(corpCode, resumeId);
            Map<String, Object> ResumeSelfEvaluation = resumeSelfEvaluationLogic.findOneMap(corpCode, resumeId);
            Map<String, Object> jobIntention = resumeJobIntentionLogic.findOneMap(corpCode, resumeId);

            persInfo.remove("id");
            persInfo.remove("resume_id");
            ResumeSelfEvaluation.remove("id");
            ResumeSelfEvaluation.remove("resume_id");
            jobIntention.remove("id");
            jobIntention.remove("resume_id");
            dataMap.putAll(persInfo);
            dataMap.putAll(ResumeSelfEvaluation);
            dataMap.putAll(jobIntention);

            List<Map<String, Object>> works = resumeWorkLogic.findObjListByResumeId(corpCode, resumeId);
            List<Map<String, Object>> edus = resumeEduLogic.findObjListByResumeId(corpCode, resumeId);
            List<Map<String, Object>> projects = resumeProjectLogic.findObjListByResumeId(corpCode, resumeId);

            if (CollectionUtils.isNotEmpty(works)) {
                for (Map<String, Object> map : works) {
                    map.remove("id");
                    map.remove("resume_id");
                    if (isClearDuty) {
                        map.remove("duty");
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(edus)) {
                for (Map<String, Object> map : edus) {
                    map.remove("id");
                    map.remove("resume_id");
                }
            }
            if (CollectionUtils.isNotEmpty(projects)) {
                for (Map<String, Object> map : projects) {
                    if (isClearDuty) {
                        map.remove("duty");
                        map.remove("description");
                    }
                    map.remove("id");
                    map.remove("resume_id");
                }
            }
            dataMap.put("works", works);
            dataMap.put("educations", edus);
            dataMap.put("projects", projects);
        }

        return dataMap;
    }


}
